{$CLEO .qa}
{$USE CLEO+}
{$USE newOpcodes}
{$USE ini}

0000: NOP

script_name "PostMan"

// By ArtemQa146

const
    Postbox = 0@
    PostMan = 29@
    Render_Obj = 28@
    Char_Check = 27@
    Process = 26@
    RenderMail = 25@
    Pause = 23@
    PostBike = 22@
    Throw_Object_Qa = 20@
    Zone_Check = 18@
    Pause_End = 60000
    Radius_Out = 100.0
    Bike_Speed = 8.0
    
    Postbag = 31@
    Postmail = 30@
    Postpaper = 21@
    Ped_ID = 24@
    Ped_ID_Bike = 19@
end

// -2119.0273 -2509.665 30.625
// -2194.8047 -2388.3215 30.625

// 31@, 30@, 21@    ini
// 24@, 19@, 21@    ini

0085: Postbag = 0@ // (int)
0085: Postmail = 1@ // (int)
0085: Postpaper = 2@ // (int)
0085: Ped_ID = 3@ // (int)
0085: Ped_ID_Bike = 4@ // (int)

while true
wait 100
    if
        player.Defined(0)
    then

            0209: 1@ = random_int_in_ranges 1 3
            if
                1@ == 1 //  2    else  
            then
                04C4: store_coords_to 1@ 2@ 3@ from_actor $PLAYER_ACTOR with_offset 0.0 -50.0 0.0
                02C0: store_to 1@ 2@ 3@ ped_path_coords_closest_to 1@ 2@ 3@
            if
                not locate_char_distance_to_coordinates $PLAYER_ACTOR pos 1@ 2@ 3@ radius 30.0
            then
            
                repeat
                wait 0
                04ED: load_animation  "PedEventsMod"
                until 04EE:   animation "PedEventsMod" loaded
            
                //  
                model.Load(Ped_ID)
                if
                    not model.Available(Ped_ID)
                then
                    repeat
                    wait 0
                    model.Load(Ped_ID)
                    until model.Available(Ped_ID)
                end
                038B: load_requested_models
                009A: PostMan = create_actor_pedtype 4 model Ped_ID at 1@ 2@ 3@
                0E24: fix_char PostMan ground 1 brightness 1 and_fade_in 0
                model.Destroy(Ped_ID)
                0F02:  PostMan special_model Postbag bone 3 offset 0.25 0.0 0.02 rotation 0 0 0 scale Render_Obj // bone 34 left hand, 24 right hand, 9 had, 3 tors, 4 upper tors
                0F02:  PostMan special_model Postmail bone 24 offset 0.01 0.015 0.0 rotation 0 0 0 scale RenderMail // bone 34 left hand, 24 right hand, 9 had, 3 tors, 4 upper tors
                0E37: set_render_object_scale Render_Obj 0.9 0.8 0.9
                0E31: set_render_object_visible RenderMail 0
                05DE: AS_actor PostMan walk_around_ped_path
                Process = 0
                
                repeat
                wait 0
                gosub @Char
                if
                    Char_Check == true //      
                then
                    if
                        process > 0
                    then
                        gosub @Wrong_Stay
                    end

                    //        
                    if 
                        Process == 0
                    then
                        1@ = 0
                        while 0EA9: get_any_object_no_save_recursive 1@ progress_to 1@ object_to Postbox
                            if or
                                09CC:   object Postbox model_is 1291
                                09CC:   object Postbox model_is 1258
                                09CC:   object Postbox model_is 1478
                                09CC:   object Postbox model_is 3408
                                09CC:   object Postbox model_is 3407
                            then
                                if and
                                    8366:   not object Postbox damaged
                                    0EE6: locate_char_distance_to_object PostMan object Postbox radius 6.5
                                then
                                    if or
                                        09CC:   object Postbox model_is 1478
                                        09CC:   object Postbox model_is 3408
                                        09CC:   object Postbox model_is 3407
                                    then
                                        5@ = -1.25
                                    else
                                        5@ = -1.38
                                    end
                                    0400: store_coords_to 1@ 2@ 3@ from_object Postbox with_offset 0.0 5@ 0.0
                                    0176: 4@ = object Postbox Z_angle
                                    07CD: AS_actor PostMan walk_to 1@ 2@ 3@ stop_with_angle 4@ within_radius 0.0
                                    Process = 1
                                    break
                                end
                            end
                        end
                    end
                    
                    //      ,    
                    if
                        Process == 1
                    then
                        if
                            03CA:   object Postbox exists
                        then
                            if
                                8366:   not object Postbox damaged
                            then
                                0400: store_coords_to 1@ 2@ 3@ from_object Postbox with_offset 0.0 5@ 0.5
                                if
                                    00EC:   actor PostMan sphere 0 near_point 1@ 2@ radius 0.7 0.7
                                then
                                    00A1: put_actor PostMan at 1@ 2@ 3@
                                    0176: 4@ = object Postbox Z_angle
                                    0173: set_actor PostMan Z_angle_to 4@
                                    0E24: fix_char PostMan ground 1 brightness 1 and_fade_in 0
                                    if or
                                        09CC:   object Postbox model_is 1478
                                        09CC:   object Postbox model_is 3408
                                        09CC:   object Postbox model_is 3407
                                    then
                                        0605: actor PostMan perform_animation "idle_stay" IFP "PedEventsMod" framedelta 2.0 loop 0 lockX 0 lockY 0 lockF 0 time -1
                                    else
                                        0605: actor PostMan perform_animation "idle" IFP "PedEventsMod" framedelta 2.0 loop 0 lockX 0 lockY 0 lockF 0 time -1
                                    end
                                    0E31: set_render_object_visible RenderMail 1
                                    Process = 2
                                    timera = 0
                                end
                            else
                                05DE: AS_actor PostMan walk_around_ped_path
                                Process = 0
                            end
                        else
                            05DE: AS_actor PostMan walk_around_ped_path
                            Process = 0
                        end
                    end
                    
                    //         
                    if
                        Process == 2
                    then
                        gosub @Wrong
                        1@ = 0.0
                        2@ = 0.0
                        0613: 1@ = actor PostMan animation "idle" time
                        0613: 2@ = actor PostMan animation "idle_stay" time
                        if or
                            1@ > 0.9
                            2@ > 0.9
                        then
                            0E31: set_render_object_visible RenderMail 0
                            05DE: AS_actor PostMan walk_around_ped_path
                            Pause += 1
                            timerb = 0
                            Process = 3
                        end
                    end
                    
                    //           
                    if
                        Process == 3
                    then
                        if
                            03CA:   object Postbox exists
                        then
                            if
                                not locate_char_distance_to_object PostMan object Postbox radius 7.5
                            then
                                Process = 0
                            end
                        else
                            Process = 0
                        end
                    end
                    
                    //   2 
                    if
                        Pause >= 2
                    then
                        process = -1
                        if
                            timerb > 15000
                        then
                            process = 0
                            Pause = 0
                        end
                    end


                else
                    break
                end
                until not locate_char_distance_to_char $PLAYER_ACTOR char PostMan radius Radius_Out
                    04EF: release_animation "PedEventsMod"
                    if
                        056D:   actor PostMan defined
                    then
                        01C2: remove_references_to_actor PostMan // Like turning an actor into a random pedestrian
                    end
                wait Pause_End
                0A93: terminate_this_custom_script
                end
            
            
// 
    else
        04C4: store_coords_to 1@ 2@ 3@ from_actor $PLAYER_ACTOR with_offset 0.0 -50.0 0.0
        if and
            02C1: store_to 1@ 2@ 3@ car_path_coords_closest_to 1@ 2@ 3@
            not locate_char_distance_to_coordinates $PLAYER_ACTOR pos 1@ 2@ 3@ radius 40.0
        then
            //      
            
            repeat
            wait 0
            04ED: load_animation  "PedEventsMod"
            until 04EE:   animation "PedEventsMod" loaded
            
            repeat
            wait 0
            model.Load(509)
            until model.Available(509)

            model.Load(Ped_ID_Bike)
            if
                not model.Available(Ped_ID_Bike)
            then
                repeat
                wait 0
                model.Load(Ped_ID_Bike)
                until model.Available(Ped_ID_Bike)
            end
            038B: load_requested_models
            2@ += 1.5
            1@ += 1.5
            00A5: PostBike = create_car 509 at 1@ 2@ 3@
            00AE: set_car PostBike traffic_behaviour_to 0
            00AD: set_car PostBike max_speed_to Bike_Speed
            0129: PostMan = create_actor_pedtype 4 model Ped_ID_Bike in_car PostBike driverseat
            model.Destroy(509) 
            model.Destroy(Ped_ID_Bike)
            0F02:  PostMan special_model Postbag bone 3 offset 0.24 0.02 0.0 rotation 0 0 0 scale Render_Obj // bone 34 left hand, 24 right hand, 9 had, 3 tors, 4 upper tors
            0F02:  PostMan special_model Postpaper bone 24 offset 0 0 0 rotation 0 0 0 scale RenderMail // bone 34 left hand, 24 right hand, 9 had, 3 tors, 4 upper tors
            0E37: set_render_object_scale Render_Obj 0.9 0.8 0.9
            0E31: set_render_object_visible RenderMail 0
            05D2: AS_actor PostMan run_to_and_hijack_car PostBike max_search_radius 30.0 traffic_behavior 0
            Process = 0
            timera = 0

            repeat
            wait 0
            gosub @Char_Bike
            if
                Char_Check == true //      
            then
                gosub @Char_Bike_In //      
                if 
                    process == 0
                then
                    1@ = 0
                    while 0EA9: get_any_object_no_save_recursive 1@ progress_to 1@ object_to Postbox
                        if or
                            09CC:   object Postbox model_is 1478
                            09CC:   object Postbox model_is 3408
                            09CC:   object Postbox model_is 3407
                        then
                            0407: store_coords_to 4@ 5@ 6@ from_car PostBike with_offset 4.0 0.0 0.0
                            02E3: 2@ = car PostBike speed
                            if and
                                2@ > 4.0
                                0EEC: locate_object_distance_to_coordinates Postbox pos 4@ 5@ 6@ radius 4.0
                            then
                                0605: actor PostMan perform_animation "idle_bike" IFP "PedEventsMod" framedelta 2.0 loop 0 lockX 0 lockY 0 lockF 0 time -1
                                Process = 1
                                0E31: set_render_object_visible RenderMail 1
                                timerb = 0
                                break
                            end
                        end
                    end
                end
            
            
                //     
                if
                    Process == 1
                then
                    gosub @Wrong_Bike
                    0393: actor PostMan perform_animation "idle_bike" at 2.0 times_normal_rate
                    04BA: set_car PostBike speed_to 4.0
                    1@ = 0.0
                    0613: 1@ = actor PostMan animation "idle_bike" time
                    if
                        1@ > 0.6
                    then
                        0E31: set_render_object_visible RenderMail 0
                        model.Load(2891)
                        038B: load_requested_models
                        0407: store_coords_to 1@ 2@ 3@ from_car PostBike with_offset 0.0 0.0 0.8
                        0E01: create_object_no_save 2891 at 1@ 2@ 3@ offset 0 ground 0 to Throw_Object_Qa
                        08D2: object Throw_Object_Qa scale_model 0.0
                        050E: set_object Throw_Object_Qa no_collision_with_car PostBike
                        0F04: create_render_object_to_object_from_special Throw_Object_Qa special_model Postpaper offset 0 0 -0.04 rotation 90.0 90.0 0 store_to 1@
                        0E37: set_render_object_scale 1@ 1.2 1.7 1.5
                        0407: store_coords_to 1@ 2@ 3@ from_car PostBike with_offset 0.7 0.0 1.0
                        0815: put_object Throw_Object_Qa at 1@ 2@ 3@  and_keep_rotation
                        05A1: set_object Throw_Object_Qa rotation_velocity_about_an_axis 0.0 0.0 100.0 through_center_of_body
                        01C4: remove_references_to_object Throw_Object_Qa // This object will now disappear when the player looks away
                        model.Destroy(2891)
                        Process = 2
                        timerb = 0
                    end
                end
            
            //   
            if
                Process == 2
            then
                gosub @Wrong_Bike
                04BA: set_car PostBike speed_to 4.0
                1@ = 0.0
                0613: 1@ = actor PostMan animation "idle_bike" time
                if 
                    1@ > 0.9
                then
                    00AE: set_car PostBike traffic_behaviour_to 0
                    05D2: AS_actor PostMan run_to_and_hijack_car PostBike max_search_radius 30.0 traffic_behavior 0
                    Process = 3
                end
            end
            
            //    ,      
            if
                Process == 3
            then
                if
                    not locate_char_distance_to_coordinates PostMan pos 4@ 5@ 6@ radius 10.0
                then
                    process = 0
                end
            end
            
            
            else
                break
            end
            until not locate_char_distance_to_char $PLAYER_ACTOR char PostMan radius Radius_Out
                04EF: release_animation "PedEventsMod"
                    if
                        056D:   actor PostMan defined
                    then
                        01C2: remove_references_to_actor PostMan // Like turning an actor into a random pedestrian
                    end
                        if
                            056E:   car PostBike defined
                        then
                            01C3: remove_references_to_car PostBike
                        end
                    wait Pause_End
                    0A93: terminate_this_custom_script
                end
            end
        end

end
    
// 

//  
:Char
Char_Check = false
if
    056D:   actor PostMan defined
then
    080E: get_actor PostMan ped_event_to 1@
    if and
        not 1@ == 49
        8118:   not actor PostMan dead
        0E0A: is_char_script_controlled PostMan
        851A:   not actor PostMan damaged_by_actor $PLAYER_ACTOR
        887A:   not gang_war_in_progress
    then
        Char_Check = true
    end
end
return



//        
:Wrong
if
    timera > 1000
then
    if and
        8611:   not actor PostMan performing_animation "idle"
        8611:   not actor PostMan performing_animation "idle_stay"
    then
        process = 3
    else
        timera = 0
    end
end
return


:Wrong_STay
0EFF: get_char_simplest_active_task PostMan id_to 1@ pointer_to 2@
    if and
        timera > 3000
        1@ == 203
    then
        05DE: AS_actor PostMan walk_around_ped_path
        timera = 0
    end
return


// 


//    
:Char_Bike
Char_Check = false
if and
    056E:   car PostBike defined
    056D:   actor PostMan defined
then
    if and
        8118:   not actor PostMan dead
        0E08: is_car_script_controlled PostBike
        0E0A: is_char_script_controlled PostMan
        8119:   not car PostBike wrecked
    then
        Char_Check = true
    end
end
return

//     
:Char_Bike_In
if
    80DB:   not actor PostMan in_car PostBike
then
    process = -1
    if and
        0E49: is_char_entering_any_car PostMan
        0E4A: is_char_exiting_any_car PostMan
        timera > 10000
    then
        05D2: AS_actor PostMan run_to_and_hijack_car PostBike max_search_radius 30.0 traffic_behavior 0
        timera = 0
    end
end
if
    00DB:   actor PostMan in_car PostBike
then
    00AD: set_car PostBike max_speed_to Bike_Speed
    if
        process == -1
    then
        process = 0
    end
end
return

//        
:Wrong_Bike
if
    timerb > 1000
then
    if
        8611:   not actor PostMan performing_animation "idle_bike"
    then
        05D2: AS_actor PostMan run_to_and_hijack_car PostBike max_search_radius 30.0 traffic_behavior 0
        process = 3
    else
        timerb = 0
    end
end
return



